//
//  Setting_GeneralViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 16/9/18.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "Setting_GeneralViewController.h"

#import "ConcealTableViewCell.h"
#import "Upload_ErrorDailyViewController.h"

@interface Setting_GeneralViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, copy) NSString * str_daily;

@end

@implementation Setting_GeneralViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"常规";
    
    //左返回
    UIImageView * image_LeftRetrun = [[UIImageView alloc] initWithFrame:CGRectMake(11, 31.5, 25, 25)];
    image_LeftRetrun.userInteractionEnabled = YES;
    image_LeftRetrun.image = [UIImage imageNamed:@"Left-return.png"];
    [_topView addSubview:image_LeftRetrun];
    
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_LeftRetrun.frame) - 6, 35, 35, 15)];
    lbl_return.text = @"设置";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 22, 60, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.str_daily = @"点击关闭日志";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[ConcealTableViewCell class] forCellReuseIdentifier:@"conceal"];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ConcealTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"conceal" forIndexPath:indexPath];
    
    switch (indexPath.row)
    {
        case 0:
        {
            cell.lbl_name.text = @"清除本地缓存";
        }
            break;
        case 1:
        {
            cell.lbl_name.text = @"上传日志";
        }
            break;
        case 2:
        {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.lbl_name.text = self.str_daily;
        }
            break;
        default:
            break;
    }
    
    return cell;
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.row == 0)
    {//NSLog(@"清除缓存");
        
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"" message:@"确定清空本地缓存？" preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
        UIAlertAction * action_cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alert addAction:action_cancel];
        
        UIAlertAction * action_Ok = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            [self clearCache];
        }];
        [alert addAction:action_Ok];
        
    }
    else if(indexPath.row == 1)
    {//NSLog(@"上传日志");
        
        Upload_ErrorDailyViewController * upload_ErrorDailyViewController = [[Upload_ErrorDailyViewController alloc] init];
        [self showViewController:upload_ErrorDailyViewController sender:nil];
    }
    else if(indexPath.row == 2)
    {//NSLog(@"点击关闭日志");
        if([self.str_daily isEqualToString:@"点击关闭日志"])
        {
            self.str_daily = @"点击开启日志";
            
            [SVProgressHUD setMinimumDismissTimeInterval:0.9f];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setBackgroundColor:[UIColor grayColor]];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
            [SVProgressHUD showSuccessWithStatus:@"开启成功"];
        }
        else
        {
            self.str_daily = @"点击关闭日志";
            
            [SVProgressHUD setMinimumDismissTimeInterval:0.9f];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setBackgroundColor:[UIColor grayColor]];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
            [SVProgressHUD showSuccessWithStatus:@"关闭成功"];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //刷新tableView(记住,要更新放在主线程中)
            
            [self.tableView reloadData];
        });
    }
}

#pragma mark - 清除缓存方法
-(void)clearCache
{
    dispatch_async(
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                   , ^{
                       
                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                       
                       for (NSString *p in files) {
                           NSError *error;
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                           }
                       }
                       [self performSelectorOnMainThread:@selector(clearCacheSuccess)
                                              withObject:nil waitUntilDone:YES];});
}

- (void)clearCacheSuccess
{
    [SVProgressHUD setMinimumDismissTimeInterval:0.9f];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setBackgroundColor:[UIColor grayColor]];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showSuccessWithStatus:@"清除成功"];
}



@end
